﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using FirstFloor.ModernUI.Windows.Controls;
using IsikFx.Project.ClientApp.Helpers;
using IsikFx.Project.CommonTypes.CRUD;
using IsikFx.Project.CommonTypes.User;
using IsikFx.Project.Utils;
namespace IsikFx.Project.ClientApp.Content
{
    /// <summary>
    /// Interaction logic for CustomerEntry.xaml
    /// </summary>
    public partial class CustomerEntry : UserControl
    {
        User user;
        ModernDialog dialog;
        StringBuilder validationResult;
        bool formIsValid = true;
        public CustomerEntry()
        {
            InitializeComponent();
            Loaded += CustomerEntry_Loaded;
        }

        void CustomerEntry_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = Helper.CurrentUser;
            customerName.DataContext = Helper.CurrentUser;
        }

        private bool ValidateForm()
        {
            validationResult = new StringBuilder();
            formIsValid = true;
            
            if (string.IsNullOrEmpty(customerName.Text))
            {
                validationResult.Append("* Lütfen müşteri ismini giriniz. \r\n");
                formIsValid = formIsValid & false;
            }

            if (string.IsNullOrEmpty(customerSurname.Text))
            {
                validationResult.Append("* Lütfen müşteri soyadını giriniz. \r\n");
                formIsValid = formIsValid & false;
            }

            if (string.IsNullOrEmpty(loginNo.Text))
            {
                validationResult.Append("* Lütfen login numarasını giriniz. \r\n");
                formIsValid = formIsValid & false;
            }

            if (FundDate.SelectedDate == null)
            {
                validationResult.Append("* Lütfen fon tarihi seçiniz. \r\n");
                formIsValid = formIsValid & false;
            }

            if (string.IsNullOrEmpty(fundAmount.Text))
            {
                validationResult.Append("* Lütfen fon miktarını giriniz. \r\n");
                formIsValid = formIsValid & false;
            }

            if (formIsValid)
            {
                return true;
            }

            else
            {
                dialog = new ModernDialog();
                dialog.Content = validationResult.ToString();
                dialog.Title = "Hata";
                dialog.Show();
                return false;
            }
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateForm())
            {
                dialog = new ModernDialog();
                try
                {
                    user = Helper.CurrentUser;
                    IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                    CrudResponseMessage responseMessage = client.CreateNewCustomer(Int32.Parse(user.UserID), customerName.Text, customerSurname.Text, loginNo.Text, DateTime.Now.ToShortDateString(), FundDate.SelectedDate.ToString(), fundAmount.Text, Helper.USD); 
                    
                    if (!responseMessage.HasError && responseMessage.Response.AffectedRows > 0)
                    {
                        dialog.Content = "Yeni müşteri kaydı başarılı bir şekilde oluşturuldu";
                        dialog.Title = "Sonuç";
                        Helper.ResetAllControlsInWindow(this);
                    }
                    else if(responseMessage.HasError)
                    {
                        dialog.Content = "Hata";
                        dialog.Title = "Sonuç";
                    }
                    dialog.ShowDialog();
                }
                catch (Exception ex)
                {
                    ExceptionHelper.HandleException(ex, typeof(CustomerEntry));
                    dialog.Content = "Hata";
                    dialog.Title = ex.Message;
                    dialog.ShowDialog();
                }
            }
        }
    }
}
